<?php

declare(strict_types=1);

namespace App\Controller;

use Exception;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\UserAuthorizationMiddleware;
use App\Middleware\UserPermissionMiddleware;
use App\Service\Logic\UserLogic;

/**
 * 用户-控制器
 */
#[Controller(prefix: "/api/user")]
class UserController extends AbstractController
{
        #[Inject]
        protected UserLogic $logic;

        /**
         * 登录
         * @return ResponseInterface
         * @throws Exception
         */
        #[RequestMapping(path: "loginUsername", methods: "post")]
        public function loginUsername(): ResponseInterface
	{
		return $this->response->json($this->logic->loginUsername());
	}

        /**
         * 获取当前登录者信息
         * @return ResponseInterface
         */
        #[RequestMapping(path: "info", methods: "get")]
        #[Middleware(UserAuthorizationMiddleware::class)]
        public function info(): ResponseInterface
        {
                return $this->response->json($this->logic->info());
        }

        /**
         * 分页
         * @return ResponseInterface
         * @throws Exception
         */
        #[RequestMapping(path: "", methods: "get")]
        public function paginate(): ResponseInterface
        {
                return $this->response->json($this->logic->paginate());
        }

        /**
         * 创建
         * @return ResponseInterface
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        #[RequestMapping(path: "", methods: "post")]
        #[Middleware(UserAuthorizationMiddleware::class)]
        #[Middleware(UserPermissionMiddleware::class)]
        public function create(): ResponseInterface
        {
                return $this->response->json($this->logic->create());
        }

        /**
         * 更新
         * @return ResponseInterface
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        #[RequestMapping(path: "", methods: "put")]
        #[Middleware(UserAuthorizationMiddleware::class)]
        #[Middleware(UserPermissionMiddleware::class)]
        public function update(): ResponseInterface
        {
                return $this->response->json($this->logic->update());
        }

        /**
         * 更新密码
         * @return ResponseInterface
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        #[RequestMapping(path: "password", methods: "put")]
        #[Middleware(UserAuthorizationMiddleware::class)]
        #[Middleware(UserPermissionMiddleware::class)]
        public function updatePassword(): ResponseInterface
        {
                return $this->response->json($this->logic->updatePassword());
        }
}
